import java.util.HashMap;
import java.util.Map;

public class OrderStateMachine {

    private OrderState currentState;

    // 状态转换规则
    private final Map<OrderState, Map<OrderEvent, OrderState>> transitions;

    public OrderStateMachine() {
        this.currentState = OrderState.CREATED;

        // 定义状态转换规则
        transitions = new HashMap<>();
        
        // 状态：CREATED
        Map<OrderEvent, OrderState> createdTransitions = new HashMap<>();
        createdTransitions.put(OrderEvent.PAY, OrderState.PAID);
        transitions.put(OrderState.CREATED, createdTransitions);
        
        // 状态：PAID
        Map<OrderEvent, OrderState> paidTransitions = new HashMap<>();
        paidTransitions.put(OrderEvent.SHIP, OrderState.SHIPPED);
        transitions.put(OrderState.PAID, paidTransitions);
        
        // 状态：SHIPPED
        Map<OrderEvent, OrderState> shippedTransitions = new HashMap<>();
        shippedTransitions.put(OrderEvent.DELIVER, OrderState.DELIVERED);
        transitions.put(OrderState.SHIPPED, shippedTransitions);
    }

    public void onEvent(OrderEvent event) {
        // 查找当前状态是否允许该事件发生
        Map<OrderEvent, OrderState> eventMap = transitions.get(currentState);
        
        if (eventMap != null && eventMap.containsKey(event)) {
            OrderState newState = eventMap.get(event);
            System.out.println("Transitioning from " + currentState + " to " + newState);
            currentState = newState;
        } else {
            System.out.println("Invalid transition from " + currentState + " with event " + event);
        }
    }

    public OrderState getCurrentState() {
        return currentState;
    }
}
